package com.seleniti.enpareja.web.desktop.client.control.controller;

import java.util.Date;
import java.util.logging.Logger;

import org.ioc.commons.flowcontrol.activitymapper.IsActivityManager;
import org.ioc.commons.flowcontrol.activitymapper.ProvidesActivityManager;
import org.ioc.commons.impl.gwt.client.flowcontrol.activitymapper.GWTActivityManager;
import org.ioc.commons.impl.gwt.client.flowcontrol.eventbus.GwtEventBusImpl;
import org.ioc.commons.impl.gwt.client.flowcontrol.operationmanager.GwtOperationManagerImpl;
import org.ioc.commons.utils.FormatterLogger;

import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.logging.client.HasWidgetsLogHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.seleniti.enpareja.controller.common.GlobalData;
import com.seleniti.enpareja.controller.control.controller.EnParejaAppControllerImpl;
import com.seleniti.enpareja.controller.control.factory.ClientFactory;
import com.seleniti.enpareja.controller.control.factory.EnParejaAppController;
import com.seleniti.enpareja.web.appbase.client.model.UserAgentResolver;
import com.seleniti.enpareja.web.desktop.client.control.navigation.DesktopActivityMapper;

public class DesktopEnParejaAppControllerImpl extends EnParejaAppControllerImpl {

	protected final com.google.web.bindery.event.shared.EventBus gwtEventBus = new SimpleEventBus();

	public DesktopEnParejaAppControllerImpl(ClientFactory clientFactory) {
		super(clientFactory);
		this.operationManager = new GwtOperationManagerImpl<EnParejaAppController.Operation>(
				this.gwtEventBus, this);
		this.eventBus = new GwtEventBusImpl<EnParejaAppController.Event>(gwtEventBus, this);
	}

	private static final FormatterLogger logger = FormatterLogger.getLogger(DesktopEnParejaAppControllerImpl.class);

	@Override
	public IsActivityManager getActivityManager() {
		ActivityMapper appActivityMapper = new DesktopActivityMapper(this.clientFactory);
		com.google.web.bindery.event.shared.EventBus gwtGlobalEventBus = (com.google.web.bindery.event.shared.EventBus) this.globalEventBus
				.asEventBus();
		GWTActivityManager activityManager = new GWTActivityManager(appActivityMapper, gwtGlobalEventBus);

		return activityManager;
	}

	@Override
	public void onRequiredGlobalDataReady() {
		if (this.parameters != null && this.parameters.forceLog) {

			VerticalPanel customLogArea = new VerticalPanel();
			DialogBox.CaptionImpl caption = new DialogBox.CaptionImpl();
			caption.setText("Logger");
			DialogBox db = new DialogBox(false, false, caption);
			db.setGlassEnabled(false);
			ScrollPanel sp = new ScrollPanel();
			sp.setSize("250px", "300px");
			sp.setWidget(customLogArea);
			db.setWidget(sp);
			db.show();

			/* an example of using own custom logging area. */
			Logger.getLogger("").addHandler(new HasWidgetsLogHandler(customLogArea));
		}

		super.onRequiredGlobalDataReady();
	}
	
	@Override
	protected void initGlobalDataProviders() {
		super.initGlobalDataProviders();
		
		this.globalDataController.setGlobalDataConstantValue(GlobalData.UserAgent, UserAgentResolver.getUserAgent());
	}

	@Override
	public void reload() {
		UrlBuilder urlB = Window.Location.createUrlBuilder();
		urlB.setHash("");
		urlB.setParameter("forceReload", String.valueOf(new Date().getTime()));
		Window.Location.replace(urlB.buildString());
	}

	@Override
	protected Object getGlobalProviderCaller() {
		return this;
	}
}
